package net.anzhuo.common.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.anzhuo.entity.es.Result;

public class SortUtils {
	
	public void quickSortTime(Result[] a, int start, int end) {
		if (start < end) {
			long baseNum = getDateTimeLong(a[start].getStartDate());// 选基准值
			Result midNum;// 记录中间值
			int i = start;
			int j = end;
			do {
				while ((getDateTimeLong(a[i].getStartDate()) < baseNum) && i < end) {
					i++;
				}
				while ((getDateTimeLong(a[j].getStartDate()) > baseNum) && j > start) {
					j--;
				}
				if (i <= j) {
					midNum = a[i];
					a[i] = a[j];
					a[j] = midNum;
					i++;
					j--;
				}
			} while (i <= j);
			if (start < j) {
				quickSortTime(a, start, j);
			}
			if (end > i) {
				quickSortTime(a, i, end);
			}
		}
	}
	
	public void quickSortCap(Result[] a, int start, int end) {
		if (start < end) {
			double baseNum = getCap(a[start].getRegCap());// 选基准值
			Result midNum;// 记录中间值
			int i = start;
			int j = end;
			do {
				while ((getCap(a[i].getRegCap())< baseNum) && i < end) {
					i++;
				}
				while ((getCap(a[j].getRegCap()) > baseNum) && j > start) {
					j--;
				}
				if (i <= j) {
					midNum = a[i];
					a[i] = a[j];
					a[j] = midNum;
					i++;
					j--;
				}
			} while (i <= j);
			if (start < j) {
				quickSortCap(a, start, j);
			}
			if (end > i) {
				quickSortCap(a, i, end);
			}
		}
	}

	public List<Result> result = new ArrayList<Result>();

	public SortUtils(List<Result> list, String flag) {

		result = new ArrayList<Result>();
		long[] arra=new long[list.size()];
		
		if (flag.equals("01")) {
		
			Result[] c=new Result[list.size()];
			list.toArray(c);
			quickSortTime(c,0,arra.length-1);
			for(int i=0;i<c.length;i++)
			{
				result.add(c[i]);
			}
			
		} else if (flag.equals("02")) {
			
			Result[] c=new Result[list.size()];
			list.toArray(c);
			quickSortTime(c,0,arra.length-1);
			for(int i=c.length-1;i>=0;i--)
			{
				result.add(c[i]);
			}
			
		} else if (flag.equals("03")) {
			Result[] c=new Result[list.size()];
			list.toArray(c);
			quickSortCap(c,0,arra.length-1);
			for(int i=0;i<c.length;i++)
			{
				result.add(c[i]);
			}
		} else {
			Result[] c=new Result[list.size()];
			list.toArray(c);
			quickSortCap(c,0,arra.length-1);
			for(int i=c.length-1;i>=0;i--)
			{
				result.add(c[i]);
			}
		}
	}

	public Double getCap(String cap) {
		Double d = 0d;
		if (cap != null && !cap.equals("")) {
			cap = cap.replace("万", "");
			cap = cap.replace("人民币", "");
			cap = cap.replace("元", "");
			cap=cap.replace("未公开", "0");
			cap=cap.replace(" ", "");
			cap=cap.replace("(","");
			cap=cap.replace(")","");
			cap=cap.replace("","0");
			try {
				d = Double.parseDouble(cap);
			} catch (Exception e) {
				return d = 0d;
			}
		} else {
			d = 0d;
		}
		return d;
	}

	public static long getDateTimeLong(String str) {
		long result = 0L;
		if(str==null||str.equals("")||str.equals("未公开"))
		{
			return result;
		}
		DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date date = format1.parse(str);
			result = date.getTime();

		} catch (Exception e) {
			return result = 0;
		}
		return result;
	}
}
